﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.Serialization;

namespace Esponce.QRCode
{
	/// <summary>
	/// Model for import/export record
	/// </summary>
	[DataContract(Name = "item", Namespace = "")]
	public class TrackDataModel : INotifyPropertyChanged
	{
		#region Constants
		public const string DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
		#endregion

		#region Public properties
		/// <summary>
		/// Gets or sets QR Code name.
		/// </summary>
		[DataMember(Name = "name", IsRequired = false, EmitDefaultValue = false, Order = 1)]
		public string Name
		{
			get
			{
				return this.name;
			}
			set
			{
				if (this.name != value)
				{
					this.name = value;
					OnPropertyChanged("Name");
				}
			}
		}
		private string name;

		/// <summary>
		/// Gets or sets campaign name.
		/// </summary>
		[DataMember(Name = "campaign", IsRequired = false, EmitDefaultValue = false, Order = 2)]
		public string CampaignName
		{
			get
			{
				return this.campaignName;
			}
			set
			{
				if (this.campaignName != value)
				{
					this.campaignName = value;
					OnPropertyChanged("CampaignName");
				}
			}
		}
		private string campaignName;

		/// <summary>
		/// Gets or sets QR Code content.
		/// </summary>
		[DataMember(Name = "content", IsRequired = false, EmitDefaultValue = false, Order = 3)]
		public string Content
		{
			get
			{
				return this.content;
			}
			set
			{
				if (this.content != value)
				{
					this.content = value;
					OnPropertyChanged("Content");
				}
			}
		}
		private string content;

		/// <summary>
		/// Gets or sets short URL for tracking.
		/// </summary>
		[DataMember(Name = "trackUrl", IsRequired = false, EmitDefaultValue = false, Order = 4)]
		public string TrackUrl
		{
			get
			{
				return this.trackUrl;
			}
			set
			{
				if (this.trackUrl != value)
				{
					this.trackUrl = value;
					OnPropertyChanged("TrackUrl");
				}
			}
		}
		private string trackUrl;

		[DataMember(Name = "created", IsRequired = false, EmitDefaultValue = false, Order = 5)]
		public string CreatedString
		{
			get
			{
				return (this.Created != null ? this.Created.Value.ToString(DateTimeFormat) : null);
			}
			set
			{
				DateTime result;
				if (DateTime.TryParseExact(value ?? "", DateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
				{
					this.Created = result;
				}
				else
				{
					this.Created = null;
				}
			}
		}

		/// <summary>
		/// Gets or sets date and time when the QR Code has been created.
		/// </summary>
		public DateTime? Created
		{
			get
			{
				return this.created;
			}
			set
			{
				if (this.created != value)
				{
					this.created = value;
					OnPropertyChanged("Created");
					OnPropertyChanged("CreatedString");
				}
			}
		}
		private DateTime? created;

		/// <summary>
		/// Gets or sets number of scans.
		/// </summary>
		[DataMember(Name = "scans", IsRequired = false, EmitDefaultValue = false, Order = 6)]
		public int Scans
		{
			get
			{
				return this.scans;
			}
			set
			{
				if (this.scans != value)
				{
					this.scans = value;
					OnPropertyChanged("Scans");
				}
			}
		}
		private int scans;

		/// <summary>
		/// Gets or sets user's comment.
		/// </summary>
		[DataMember(Name = "comments", IsRequired = false, EmitDefaultValue = false, Order = 7)]
		public string Comments
		{
			get
			{
				return this.comments;
			}
			set
			{
				if (this.comments != value)
				{
					this.comments = value;
					OnPropertyChanged("Comments");
				}
			}
		}
		private string comments;

		/// <summary>
		/// Gets or sets QR Code module size in pixels.
		/// </summary>
		[DataMember(Name = "size", IsRequired = false, EmitDefaultValue = false, Order = 8)]
		public int Size
		{
			get
			{
				return this.size;
			}
			set
			{
				if (this.size != value)
				{
					this.size = value;
					OnPropertyChanged("Size");
				}
			}
		}
		private int size;

		/// <summary>
		/// Gets or sets QR Code padding in modules.
		/// </summary>
		[DataMember(Name = "padding", IsRequired = false, EmitDefaultValue = false, Order = 9)]
		public int Padding
		{
			get
			{
				return this.padding;
			}
			set
			{
				if (this.padding != value)
				{
					this.padding = value;
					OnPropertyChanged("Padding");
				}
			}
		}
		private int padding;

		/// <summary>
		/// Gets or sets QR Code version.
		/// </summary>
		[DataMember(Name = "version", IsRequired = false, EmitDefaultValue = false, Order = 10)]
		public int Version
		{
			get
			{
				return this.version;
			}
			set
			{
				if (this.version != value)
				{
					this.version = value;
					OnPropertyChanged("Version");
				}
			}
		}
		private int version;

		/// <summary>
		/// Gets or sets content encode mode: Byte, Alphanumeric or Numeric.
		/// </summary>
		[DataMember(Name = "encodeMode", IsRequired = false, EmitDefaultValue = false, Order = 11)]
		public string EncodeMode
		{
			get
			{
				return this.encodeMode;
			}
			set
			{
				if (this.encodeMode != value)
				{
					this.encodeMode = value;
					OnPropertyChanged("EncodeMode");
				}
			}
		}
		private string encodeMode;

		/// <summary>
		/// Gets or sets error correction level: L, M, H, Q
		/// </summary>
		[DataMember(Name = "errorCorrection", IsRequired = false, EmitDefaultValue = false, Order = 12)]
		public string ErrorCorrection
		{
			get
			{
				return this.errorCorrection;
			}
			set
			{
				if (this.errorCorrection != value)
				{
					this.errorCorrection = value;
					OnPropertyChanged("ErrorCorrection");
				}
			}
		}
		private string errorCorrection;
		#endregion

		#region INotifyPropertyChanged Members
		public event PropertyChangedEventHandler PropertyChanged;
		protected virtual void OnPropertyChanged(string propertyName)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}
		#endregion
	}
}
